package com.ea.eadp.nexus.test.pageDefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

/**
 * Created by ChuanYe on 9/5/2014.
 */
public class ChallengeVerifyPage extends BasePage {
    protected static final String twofactorCodeInputBox = "twofactorCode";
    protected static final String securityAnswerInputBox = "securityAnswer";
    protected static final String trustThisDeviceCheckBox = "trustThisDevice";
    protected static final String getTrustThisDeviceLabel = "label-trustThisDevice";
    protected static final String resendErrorCodeId = "resend-error-code";
    protected static final String codeSentErrorMessage = "A code has already been sent. Please try to resend again after 1 min.";
    protected static final String invalidCodeErrorMessage = "Incorrect code entered";
    protected static final String throttlingIpErrorMessage = "Too many attempts, retry in a few minutes";
    protected static final String bruteForceErrorMessage = "Too many attempts, retry in a few minutes";
    protected static final String cancelButton = "btnCancel";

    public ChallengeVerifyPage(WebDriver driver) {
        super(driver, ChallengeVerifyPage.class);
    }
    public void waitPageLoadCompleted() {
        WebDriverWait wait = new WebDriverWait(driver, 30, 1000);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(twofactorCodeInputBox)));
    }

    public void inputTwoFactorCode(String code) {
        sendInput(twofactorCodeInputBox, code);
    }

    public void inputSecurityAnswer(String Answer) {
        sendInput(securityAnswerInputBox, Answer);
    }
    public void checkRememberDevice() {
        WebElement element =  driver.findElement(By.id(getTrustThisDeviceLabel));
        WebElement elementCheckBox = driver.findElement(By.id(trustThisDeviceCheckBox));
        //JavascriptExecutor js = (JavascriptExecutor)driver;
        //js.executeScript("document.getElementById('trustThisDevice').style.display='block'");
        // waitForWebElementPresent(By.id(trustThisDeviceCheckBox));
        //WebElement element2 = driver.findElement(By.cssSelector(".origin-ux-checkbox > .origin-ux-checkbox-control"));
        if(!elementCheckBox.isSelected()) {
            //((JavascriptExecutor)driver).executeScript("document.getElementById('trustThisDevice').checked = true;");
            element.click();
            //element2.click();
            //driver.findElement(By.className("origin-ux-checkbox-control")).click();
        }
    }
    public boolean verifycheckRememberDevice(){
        WebElement element =  driver.findElement(By.id(getTrustThisDeviceLabel));
        WebElement elementCheckBox = driver.findElement(By.id(trustThisDeviceCheckBox));
        return elementCheckBox.isSelected();
    }
    public void uncheckRememberDevice() {
        WebElement element =  driver.findElement(By.id(getTrustThisDeviceLabel));
        WebElement elementCheckBox = driver.findElement(By.id(trustThisDeviceCheckBox));
        //JavascriptExecutor js = (JavascriptExecutor)driver;
        //js.executeScript("document.getElementById('trustThisDevice').style.display='block'");
        // waitForWebElementPresent(By.id(trustThisDeviceCheckBox));
        //WebElement element2 = driver.findElement(By.cssSelector(".origin-ux-checkbox > .origin-ux-checkbox-control"));
        if(elementCheckBox.isSelected()) {
            //((JavascriptExecutor)driver).executeScript("document.getElementById('trustThisDevice').checked = true;");
            element.click();
            //element2.click();
            //driver.findElement(By.className("origin-ux-checkbox-control")).click();
        }
    }

    public void verifyTwoFactorCode() {
        driver.findElement(By.cssSelector("#btnTFAVerify > span > span")).click();
    }
    public void verifySecurityAnswer() {
        driver.findElement(By.cssSelector("#btnSQASubmit > span > span")).click();
    }
    public void verifyCodeSentErrorMessgeExist() {
        WebElement element = driver.findElement(By.id(resendErrorCodeId));
        Assert.assertNotNull(element, "not able to locate the code already sent error message");
        Assert.assertTrue(element.getText().contains(codeSentErrorMessage));
    }

    public void verifyInvalidCodeErrorMessgeExist() {
        WebElement element = driver.findElement(By.cssSelector(".origin-ux-textbox-status-message"));
        Assert.assertNotNull(element, "not able to locate the invalid code error message");
        Assert.assertTrue(element.getText().contains(invalidCodeErrorMessage));
    }

    public void verifyBruteForceErrorMessgeExist() {
        WebElement element = driver.findElement(By.cssSelector(".origin-ux-textbox-status-message"));
        Assert.assertNotNull(element, "not able to locate the brute force error message");
        Assert.assertTrue(element.getText().contains(bruteForceErrorMessage));
    }

    public void cancel() {
        driver.findElement(By.id(cancelButton)).click();
    }
    public void skipUpdateToAppAuth() {
        try {
            driver.findElement(By.id(cancelButton)).click();
        }
        catch (NoSuchElementException e){
            log.warn(e.getMessage());
        }

    }
    public void resendCode() {
        driver.findElement(By.id("resend_code_link")).click();
    }

    public void verifyBruteForceErrorMessgeBannerExistForWebFlow() {
        WebElement element = driver.findElement(By.id(resendErrorCodeId));
        Assert.assertNotNull(element, "not able to locate the brute force error message");
        Assert.assertTrue(element.getText().contains(bruteForceErrorMessage));
    }

    public void verifyBruteForceErrorMessgeBannerExistForChallengeFlow() {
        WebElement element = driver.findElement(By.id(resendErrorCodeId));
        Assert.assertNotNull(element, "not able to locate the brute force error message");
        Assert.assertTrue(element.getText().contains(bruteForceErrorMessage));
    }
    public void verifyThrottlingIpErrorMessgeBannerExistForChallengeFlow() {
        WebElement element = driver.findElement(By.id(resendErrorCodeId));
        Assert.assertNotNull(element, "not able to locate the ip throttling error message");
        Assert.assertTrue(element.getText().contains(throttlingIpErrorMessage));
    }
}
